package com.hex.ds.hdrs.common.helper;

import com.hex.ds.hdrs.common.utils.ListUtil;
import com.hex.ds.hdrs.period.job.po.JobTask;
import com.hex.ds.hdrs.period.job.po.JobTaskDef;
import com.hex.ds.hdrs.period.job.po.PeriodJob;
import com.hex.ds.hdrs.period.task.po.MutexPool;
import com.hex.ds.hdrs.period.task.po.PeriodTask;
import com.hex.ds.hdrs.period.task.po.PeriodTaskDef;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * list 处理帮助类
 */
@Slf4j
public class ListHelper {

    /**
     * 通过作业号查询
     *
     * @param jobTaskList
     * @param jobCode
     * @return
     */
    public static JobTask qryJobTask(List<JobTask> jobTaskList, String jobCode) {
        return jobTaskList.stream().filter(jobTask -> jobCode.equals(jobTask.getJobCode())).findAny().orElse(null);
    }

    /**
     * 删除list
     *
     * @param list
     * @param jobCode
     * @return
     */
    public static List<JobTask> delJobInList(List<JobTask> list, String jobCode) {
        log.debug("删除list【ListHelper.delJobInList】jobCode:{}" , jobCode);
        List<JobTask> tempList = new ArrayList<>(list.size());
        for (int i = 0; i < list.size(); i++) {
            if (!jobCode.equals(list.get(i).getJobCode())) {
                tempList.add(list.get(i));
            }
        }
        return tempList;
    }

    /**
     * list转map
     *
     * @param jobTaskList
     * @return
     */
    public static Map<String, JobTask> jobTaskListToMap(List<JobTask> jobTaskList) {
        log.debug("list转map【ListHelper.jobTaskListToMap】");
        if (ListUtil.isEmpty(jobTaskList)) {
            return new HashMap<>();
        } else {
            return jobTaskList.stream().collect(Collectors.toMap(JobTask::getJobCode, Function.identity(), (key1, key2) -> key2));
        }
    }

    /**
     * jobList  to jobMap
     *
     * @param periodJob
     * @return
     */
    public static Map<String, PeriodJob> jobListToJobMap(List<PeriodJob> periodJob) {
        log.debug("jobList to jobMap【ListHelper.jobListToJobMap】");
        if (ListUtil.isEmpty(periodJob)) {
            return new HashMap<>();
        } else {
            return periodJob.stream().collect(Collectors.toMap(PeriodJob::getJobCode, Function.identity(), (key1, key2) -> key2));
        }
    }

    /**
     * taskList转 taskCodeMap
     *
     * @param taskList
     * @return
     */
    public static Map<String, PeriodTask> taskListToTaskMap(List<PeriodTask> taskList) {
        log.debug("taskList 转 taskCodeMap【ListHelper.taskListToTaskMap】");
        if (ListUtil.isEmpty(taskList)) {
            return new HashMap<>();
        } else {
            return taskList.stream().collect(Collectors.toMap(PeriodTask::getTaskCode, Function.identity(), (key1, key2) -> key2));
        }
    }

    /**
     * taskList转 jobCodeMap
     *
     * @param taskList
     * @return
     */
    public static Map<String, List<PeriodTask>> taskListToMap(List<PeriodTask> taskList) {
        if (ListUtil.isEmpty(taskList)) {
            return new HashMap<>();
        } else {
            return taskList.stream().collect(Collectors.groupingBy(PeriodTask::getJobCode));
        }
    }

    /**
     * list转map
     *
     * @param jobTaskDefList
     * @return
     */
    public static Map<String, JobTaskDef> jobTaskDefListToMap(List<JobTaskDef> jobTaskDefList) {
        log.debug("list 转 map【ListHelper.jobTaskDefListToMap】");
        if (ListUtil.isEmpty(jobTaskDefList)) {
            return new HashMap<>();
        } else {
            return jobTaskDefList.stream().collect(Collectors.toMap(JobTaskDef::getJobCode, Function.identity(), (key1, key2) -> key2));
        }
    }

    /**
     * list转map
     *
     * @param taskDefList
     * @return
     */
    public static Map<String, PeriodTaskDef> taskDefListToMap(List<PeriodTaskDef> taskDefList) {
        log.debug("list 转 map【ListHelper.taskDefListToMap】");
        if (ListUtil.isEmpty(taskDefList)) {
            return new HashMap<>();
        } else {
            return taskDefList.stream().collect(Collectors.toMap(PeriodTaskDef::getTaskCode, Function.identity(), (key1, key2) -> key2));

        }
    }

    /**
     * taskDefList to <jobCode,taskDefList>
     *
     * @param taskDefList
     * @return
     */
    public static Map<String, List<PeriodTaskDef>> taskDefListToJobMap(List<PeriodTaskDef> taskDefList) {
        log.debug("【ListHelper.taskDefListToMap】=======》");
        if (ListUtil.isEmpty(taskDefList)) {
            return new HashMap<>();
        } else {
            return taskDefList.stream().collect(Collectors.groupingBy(PeriodTaskDef::getJobCode));
        }
    }

    /**
     * list转queue
     *
     * @param jobTaskList
     * @return
     */
    public static PriorityBlockingQueue<JobTask> listToQueue(List<JobTask> jobTaskList, Comparator<JobTask> idComparator) {
        log.debug("list 转 queue【ListHelper.listToQueue】");
        PriorityBlockingQueue<JobTask> jobTaskQueue = new PriorityBlockingQueue<>(jobTaskList.size(), idComparator);
        jobTaskQueue.addAll(jobTaskList);
        return jobTaskQueue;
    }

    /**
     * list转queue
     *
     * @param jobTaskList
     * @return
     */
    public static LinkedBlockingQueue<JobTask> listToQueue(List<JobTask> jobTaskList) {
        log.debug("list 转 queue【ListHelper.listToQueue】");
        LinkedBlockingQueue<JobTask> jobTaskQueue = new LinkedBlockingQueue<>();
        if (!ListUtil.isEmpty(jobTaskList)) {
            jobTaskQueue.addAll(jobTaskList);
        }
        return jobTaskQueue;
    }

    /**
     * 任务对象集合转 任务编号集合
     *
     * @param taskDefList
     * @return
     */
    public static List<String> taskDefListToCodeList(List<PeriodTaskDef> taskDefList) {
        if (ListUtil.isEmpty(taskDefList)) {
            return null;
        } else {
            return taskDefList.stream().map(PeriodTaskDef::getTaskCode).collect(Collectors.toList());
        }
    }

    /**
     * 任务对象实例集合转 任务编号集合
     *
     * @param taskDefList
     * @return
     */
    public static List<String> taskInfListToCodeList(List<PeriodTask> taskDefList) {
        if (ListUtil.isEmpty(taskDefList)) {
            return null;
        } else {
            return taskDefList.stream().map(PeriodTask::getTaskCode).collect(Collectors.toList());
        }
    }

    /**
     * list转map
     *
     * @param list
     * @return
     */
    public static Map<String, String> listToMap(List<String> list) {
        log.debug("list 转 map【ListHelper.listToMap】");
        Map<String, String> map = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            map.put(list.get(i), list.get(i));
        }
        return map;
    }

    /**
     * list交换位置
     *
     * @param list
     * @param srcIndex
     * @param targetIndex
     */
    public static void swap(List<String> list, int srcIndex, int targetIndex) {
        //判断list是否为空
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        //创建一个临时变量存储数据
        String src = list.get(srcIndex);
        //删除移动
        list.remove(srcIndex);
        list.add(targetIndex, src);
    }

    /**
     * 切割字符串
     *
     * @param srcData
     * @return
     */
    public static List<String> getList(String srcData) {
        if (StringUtils.isEmpty(srcData)) {
            log.debug("字符串为空【ListHelper.getList】");
            return null;
        }
        return Stream.of(srcData.split(",")).collect(Collectors.toList());
    }

    /**
     * 比较两个集合 获取增量的元素
     *
     * @param oldList
     * @param newList
     * @return
     */
    public static List<String> getAddElemtList(List<String> oldList, List<String> newList) {
        List<String> addList = new ArrayList<>();
        Map<String, String> oldMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(oldList)) {
            oldMap = ListHelper.listToMap(oldList);
        }
        if (CollectionUtils.isNotEmpty(newList)) {
            for (String e : newList) {
                if (!oldMap.containsKey(e)) {
                    addList.add(e);
                }
            }
        }
        return addList;
    }

    public static List<JobTask> getDiffJobTask(List<JobTask> oldJobList, List<JobTask> newJobList) {
        List<JobTask> diff = new ArrayList<JobTask>();
        long start = System.currentTimeMillis();
        Map<String, Integer> map = new HashMap<String, Integer>(oldJobList.size() + newJobList.size());
        Map<String, JobTask> jobTaskMap = new HashMap<String, JobTask>(oldJobList.size() + newJobList.size());
        List<JobTask> maxList = oldJobList;
        List<JobTask> minList = newJobList;
        if (newJobList.size() > oldJobList.size()) {
            maxList = newJobList;
            minList = oldJobList;
        }
        for (JobTask jobTask : maxList) {
            map.put(jobTask.getJobCode(), 1);
            jobTaskMap.put(jobTask.getJobCode(), jobTask);
        }
        for (JobTask jobTask : minList) {
            String jobCode = jobTask.getJobCode();
            Integer count = map.get(jobCode);
            if (count != null && jobTask.getUpTs().equals(jobTaskMap.get(jobCode).getUpTs())) {
                map.put(jobCode, ++count);
                continue;
            }
            map.put(jobCode, 1);
            jobTaskMap.put(jobCode, jobTask);
        }
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            if (entry.getValue() == 1) {
                diff.add(jobTaskMap.get(entry.getKey()));
            }
        }
        log.debug("耗时：{} 毫秒", (System.currentTimeMillis() - start));
        return diff;
    }
}

